package cn.itsource.ymcc.producer;

import cn.itsource.ymcc.common.constant.RocketConstants;
import cn.itsource.ymcc.domain.PayOrder;
import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class OrderProducer {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    public void notice(String message){
        rocketMQTemplate.sendOneWay(RocketConstants.COURSE_SEND_SYSTEM_TOPIC+":"+
                RocketConstants.COURSE_SEND_SYSTEM_TAG, message);
    }

    public void createPayOrderTransaction(Map<String,Object> param, PayOrder payOrder){
        //第一参数  要监听的名字
        //第二参数   topic+tag  消费者要用的名字
        //第三参数   要发送的消息
        //第四参数   本地事务要用的数据
        rocketMQTemplate.sendMessageInTransaction(
            RocketConstants.MQ_COURSEORDER_PAY_GROUP_TRANSACTION,
                RocketConstants.ORDER_SEND_TRANSACTION_TOPIC+":"+RocketConstants.ORDER_SEND_TRANSACTION_TAG,
                MessageBuilder.withPayload(JSON.toJSONString(payOrder)).build(),
                param
        );
    }
}
